package com.su.mw.mscthesis.servlets;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jbpm.api.ProcessEngine;
import org.jbpm.api.TaskService;

import com.su.mw.mscthesis.util.MScThesisUtils;

/**
 * Servlet implementation class SubmitThesisReview
 */
public class SubmitThesisReviewServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ProcessEngine engine = MScThesisUtils.PROCESS_ENGINE;
		TaskService ts = engine.getTaskService();
		String taskId = request.getParameter("taskId");
		if ("Cancel".equals(request.getParameter("action"))) {
			//give up task
			ts.assignTask(taskId, null);
		} else {
			//complete task
			Map<String, Object> variables = new HashMap<String, Object>();
			variables.put("comments", request.getParameter("comments"));
			variables.put("conclusion", request.getParameter("conclusion"));
			variables.put("contributions", request.getParameter("contributions"));
			variables.put("briefDescription", request.getParameter("briefDescription"));
			variables.put("layout", request.getParameter("layout"));
			variables.put("mark", Integer.valueOf(request.getParameter("mark")));
			variables.put("recommendations", request.getParameter("recommendations"));
			variables.put("relevance", request.getParameter("relevance"));
			variables.put("reviewer", request.getSession().getAttribute("userid"));
			ts.setVariables(taskId, variables);
			ts.completeTask(taskId);
		}
	}
	
}
